package com.javaee.controlller;

import com.javaee.domain.Activity;
import com.javaee.service.impl.activityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("activity")
public class activityController {

    @Autowired
   private activityServiceImpl activityService;

    @GetMapping()
    public String showActivities(Model model) {
        List<Activity> activities = activityService.getAll(); // 获取所有活动
        model.addAttribute("activityList", activities);
        return "query-activities"; //
    }
    @GetMapping("{id}")
    public String query(@PathVariable int id ,Model model) {
        List<Activity> activities = activityService.getAll();
        Activity activity1 = (activityService.query(id));
        model.addAttribute("activityList", activities);
        if (activity1 != null) {
            model.addAttribute("activity1", activity1);
        }
        else model.addAttribute("activity1", "没有此活动id");
        return "query-activities";
    }

    @GetMapping("/add")
    public String addActivities(Model model) {
        return "add-activities";
    }

    @PostMapping("/add")
    public String add(@ModelAttribute Activity activity, Model model) {
        String result = activityService.add(activity);
        if (result.equals("活动添加成功")) {
            model.addAttribute("msg", result);
            return "redirect:/activity";
        } else {
            model.addAttribute("msg", result);
            return "add-activities";
        }
    }
}
